package com.kaka.bus.engine.borrow.phone;

import android.content.Intent;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.api.RetrofitUtils;
import com.kaka.bus.base.RxBaseActivity;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.engine.home.main.limit.LimitActivity;
import com.kaka.bus.engine.other.DrainageActivity;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.util.U;
import com.kaka.bus.view.dialog.GifDialog;
import com.kaka.bus.view.dialog.MessageDialog;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by zhangqi on 2016/10/18.
 * e-mail : ${email}
 * desc :
 */

public class PhoneYzmActivity2 extends RxBaseActivity {

    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_phoneyzm2_tips)
    TextView mTvPhoneyzm2Tips;
    @BindView(R.id.edt_phoneyzm_yzm2)
    EditText mEdtPhoneyzmYzm2;
    @BindView(R.id.btn_phoneyzm2_commit)
    AppCompatButton mBtnPhoneyzm2Commit;

    private String mId;
    private GifDialog mGifDialog;
    private MessageDialog mDialog;

    @Override
    protected void initView() {
        mToolbarName.setText("手机认证");
        mToolbarBack.setOnClickListener(v -> finish());
        String tips = getString(R.string.phoneyzm_tips);
        mTvPhoneyzm2Tips.setText(tips);

        mGifDialog = new GifDialog(this);
        mDialog = new MessageDialog(this);
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        mId = intent.getStringExtra(PhoneActivity.ID);
    }

    @Override
    protected void initListener() {
        mBtnPhoneyzm2Commit.setOnClickListener(v -> {
            String yzm = mEdtPhoneyzmYzm2.getText().toString();
            if(TextUtils.isEmpty(yzm)){
                showMessage("短信验证码不能为空");
                return;
            }

            sendService(yzm);
        });
    }

    private void sendService(String yzm) {
        Map<String,String> map = new HashMap<>();
        map.put(Constant.USERID,getUserId());
        map.put("command_code",mId);
        map.put("verify_code",yzm);
        map.put("token",getToken());
        map.put("md5sign", U.Sign(map));
        showDialog("提交验证码");
        addSubscription(RetrofitUtils.getInstance().build()
                .commitPhonePwdRc(map)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<String>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<String> data) {
                        onResult(data);
                    }

                    @Override
                    protected void onFinish() {
                        dissDialog();
                    }
                }));
    }

    private void onResult(HttpWrapper<String> o) {
        switch (o.getCode()){
            case "8888":
            case "10008":
            case "0000":
                startActivity(new Intent(this, LimitActivity.class));
                break;
            case "1000":
                U.toDrainage(this, DrainageActivity.TYPE_REFUSE,Constant.REFUES);
                break;
            case "2000":
                /**
                 * 展示信息
                 */
                mDialog.show();
                mDialog.setMessage(o.getInfo());
                break;
            case "10001":
                /**
                 * 展示信息
                 */
                break;
            case Constant.TOKEN_TIMEOUT:
                U.tokenOut(this);
                break;
        }
    }

    @Override
    protected boolean needFullScreen() {
        return true;
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_phoneyzm2;
    }

}
